<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/8
 * Time: 17:15
 */

namespace app\admin\controller;

use app\admin\validate\Admin as validateAdmin;
use think\Controller;
use think\facade\Session;

class User extends Controller {
    public function __construct(){
        parent::__construct();
    }

    public function login(){
        Session::clear();
        return view();
    }

    public function save(){
        $admin = new validateAdmin;
        $data = request()->post();
        if(!$admin->scene('save')->check($data)){
            return [
                'status'=>0,
                'msg'=>$admin->getError()
            ];
        }
        $admin = db('admin')
            ->field('id')
            ->where('username','eq',$data['username'])
            ->find();
        if(!$admin){
            return [
                'status'=>0,
                'msg'=>'管理员不存在'
            ];
        }
        $pwd = $this->_password($data['password']);
        if(!db('admin')->update([
            'id'=>$admin['id'],
            'password'=>$pwd,
            'dates'=>time()
        ])){
            return [
                'status'=>0,
                'msg'=>'修改失败'
            ];
        }
        return [
            'status'=>1,
            'msg'=>'修改成功'
        ];
    }

    public function logon(){
        $admin = new validateAdmin;
        $data = request()->param();

        if(!isset($data['switch'])){
            unset($data['pass']);
            $admin = new \app\admin\model\Mch();
            $_admin = $admin::where('name|tel','eq',trim($data['username']))->find();
            if(!$_admin){
                return ['status'=>0,'msg'=>'账号不存在'];
            }
            $flag = $this->__password($data['password'])==$_admin['password'];
            if(!$flag){
                return ['status'=>0,'msg'=>'密码错误'];
            }
            if($_admin['status']==-1){
                return ['status'=>0,'msg'=>'账号已被锁定'];
            }
            $location = \itbdw\Ip\IpLocation::getLocation(request()->ip());
            $address = "";
            if($location['country']){
                $address .= ",{$location['country']}";
            }
            if($location['province']){
                $address .= ",{$location['province']}";
            }
            if($location['city']){
                $address .= ",{$location['city']}";
            }
            if($location['area']){
                $address .= ",{$location['area']}";
            }
            if($location['isp']){
                $address .= ",{$location['isp']}";
            }
            $_admin->last_ip = $location['ip'];
            $_admin->last_date = time();
            $_admin->last_address=substr($address,1);
            $_admin->save();
            //保存登录状态
            Session('mc_id',$_admin['id']);
            Session('is_vip',$_admin['is_vip']);
            Session('_id',$_admin['id']);
            session('_gid',$_admin['gid']);
            Session('_name',ucfirst($_admin['name']));
            Session('_login',$_admin);
            return ['status'=>1,'msg'=>'登陆成功','data'=>$_admin];
        }else{
            unset($data['pass']);
            if(!$admin->scene('login')->check($data)){
                return [
                    'status'=>0,
                    'msg'=>$admin->getError()
                ];
            }
            $admin = new \app\admin\model\Admin;
            $_admin = $admin::where('username','eq',$data['username'])->find();
            if(!$_admin){
                return ['status'=>0,'msg'=>'账号不存在'];
            }

            $flag = $this->_password($data['password'])==$_admin['password'];
            if(!$flag){
                return ['status'=>0,'msg'=>'密码错误'];
            }
            if($_admin['status']!=0){
                return ['status'=>0,'msg'=>'账号已被锁定'];
            }
            unset($_admin['password']);
            unset($_admin['hash']);
            unset($_admin['update_time']);
            $location = \itbdw\Ip\IpLocation::getLocation(request()->ip());
            $address = "";
            if($location['country']){
                $address .= ",{$location['country']}";
            }
            if($location['province']){
                $address .= ",{$location['province']}";
            }
            if($location['city']){
                $address .= ",{$location['city']}";
            }
            if($location['area']){
                $address .= ",{$location['area']}";
            }
            if($location['isp']){
                $address .= ",{$location['isp']}";
            }
            $_admin->last_ip = $location['ip'];
            $_admin->last_date = time();
            $_admin->last_address=substr($address,1);
            $_admin->save();
            //保存登录状态
            Session('_id',$_admin['id']);
            session('_gid',$_admin['gid']);
            Session('_name',ucfirst($_admin['username']));
            Session('_login',$_admin);
            return ['status'=>1,'msg'=>'登陆成功','data'=>$_admin];
        }
    }

    protected function _password($password,$flag=false){
        return $flag?substr($password,10,15):
            substr(md5($password),10,15);
    }

    public function logout(){
        Session::clear();
        return ['status'=>1,'msg'=>'退出成功'];
    }


    protected static function __password($password){
        return substr(md5($password),10,20);
    }

}